package com.aboo.es.action;

import com.aboo.es.service.ActorService;
import com.aboo.es.vo.Actor;
import com.aboo.frame.vo.ReturnVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/es")
public class ActorAction {

    @Resource
    private ActorService actorService;

    @RequestMapping("/actor/createIndex")
    public ReturnVO<Object> createIndex(HttpServletRequest request) {
        ReturnVO<Object> rv = new ReturnVO<>();
        actorService.createIndex();

        return rv;
    }

    @RequestMapping("/actor/saveOne")
    public ReturnVO<Object> saveOne(HttpServletRequest request) {
        ReturnVO<Object> rv = new ReturnVO<>();
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String desc = request.getParameter("desc");
        Actor actor = new Actor();
        actor.setActorCode(UUID.randomUUID().toString());
        actor.setName(name);
        actor.setSex(sex);
        actor.setDesc(desc);
        actorService.saveOne(actor);

        return rv;
    }

    @RequestMapping("/actor/queryStrPage")
    public ReturnVO<Page<Actor>> queryStrPage(HttpServletRequest request) {
        ReturnVO<Page<Actor>> rv = new ReturnVO<>();
        String queryStr = request.getParameter("queryStr");
        Page<Actor> pageResult = actorService.queryPage(queryStr, 0, 20, Sort.Direction.DESC, "createdDate");

        rv.setData(pageResult);
        return rv;
    }

}
